package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;

import domain.Message;
import service.IMessageService;

@Controller
public class MessageController {
	@Autowired
	private IMessageService service;

	@RequestMapping("/msg") //判断是否登录 并分页跳往GuestBook界面  
	public String goMsg(HttpServletRequest req, Integer page) {
		//避免直接访问页面  这其实应该交给拦截器 来做 但是我的 拦截器  没写好(4.15写好了)
		/*	if (req.getSession().getAttribute("ADMIN_IN_SESSION") == null) {
				//返回登录页面
				return "admin/login";
			}*/
		Integer pageSize = 7;
		if (page == null) {
			page = 1; //默认第一页开始
		}
		List<Message> all = service.listAll(page, pageSize);
		PageInfo<Message> pageInfo = new PageInfo(all);
		req.setAttribute("pageInfo", pageInfo);
		req.setAttribute("message", all);
		return "admin/guestbook";
	}

	@RequestMapping("/msgDelete")
	public void delete(HttpServletRequest req, HttpServletResponse resp, Long id) throws Exception {
		Integer state = service.delete(id);
		if (state > 0) {
			req.setAttribute("isSuccess", "删除成功");
		} else {
			req.setAttribute("isSuccess", "删除失败");
		}
		req.getRequestDispatcher("/msg").forward(req, resp);
	}
}
